<?php 

session_start(); 

require_once "lib/model.inc";
require_once "lib/template.inc";

$stash[current_login] = $_SESSION[current_login];

/*
var_dump($_GET);
echo "<br><br>";
var_dump($_POST);
*/

if ($stash[current_login]) {
    $stash[show_add_form] = 1;
} else {
    $stash[auth_required] = 1;
}

/* редактирование */
if ($_GET[event_id] ) {
    $event =  get_events( $_GET[event_id] );

    #$event = $event[0]; // TODO хака!

    if ($event) {
        while (list ($key,$var) = each( $event )) {
            $stash[$key] = $var;
        }
    }
}


if ($_POST) {
    while (list ($key,$var) = each( $_POST )) {
        if ($var)  {
            $data[$key] = $var;
        }
    }
}

/* заполняем custom поля -- организатор */
if ( !$data[manager] ) {
    $data[manager] = $data[manager_custom];
    unset($data[manager_custom]);

    if ($data[manager]) {
        add_new_manager($data[manager]);
    }
}

/* заполняем custom поля -- места */
if (!$data[place]) {
    $data[place] = $data[place_custom];
    unset($data[place_custom]);

    if ($data[place]) {
        add_new_place($data[place]);
    }
}

/* заполняем custom поля -- адреса */
if (!$data[address]) {
    $data[address] = $data[address_custom];
    unset($data[address_custom]);

    if ($data[address]) {
        add_new_address($data[address]);
    }
}

if ($_POST && !add_event( $data )) {
    //var_dump($data);

    global $active_db;
    echo mysql_error($active_db);

    $stash[not_all_fields_filled] = 1;
}

$stash[places]    = get_places();
$stash[managers]  = get_managers();
$stash[addresses] = get_addresses();


echo get_template('add_event', $stash);
?>

